﻿#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "formmaincenter.h"
#include "qpainter.h"
#include "dlgadvancesetting.h"
#include <QtCore/qmath.h>
#include "qwininterface.h"
#include <QMessageBox.h>
#include <Windows.h>
#include <QWidget>
#include "MessageThread.h"
#include <qtimer.h>
#include "qdebug.h"
#include <QPushButton>
#include <qcombobox.h>
#include "rufus.h"
#include "drive.h"
#include <QTextCodec>
#include "cosapi.h"
#include <string.h>
using namespace std;
#include "downloadThread.h"
//#include "cos/cos_api.h"
//#include "cos/cos_sys_config.h"
//#include "cos/cos_defines.h"


MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{   
    ui->setupUi(this);
    centerWidget = new FormMainCenter(this);
    setCentralWidget(centerWidget);
    this->setWindowFlags(Qt::FramelessWindowHint);//去掉标题栏
    this->setWindowIcon(QIcon(":/images/t1.png"));
    progressBar = this->findChild<QProgressBar*>("progressBar");
    uefiPushButton=this->findChild<QPushButton*>("pushButton_zipfat32");
    pushButton_ModeDefault = this->findChild<QPushButton*>("pushButton_ModeDefault");
    pushButton_ModeISO = this->findChild<QPushButton*>("pushButton_ModeISO");
    label_currentByte_value = this->findChild<QLabel*>("label_currentByte_value");
    label_countSize_value = this->findChild<QLabel*>("label_countSize_value");
    label_currentByte= this->findChild<QLabel*>("label_currentByte");
    label_countSize = this->findChild<QLabel*>("label_countSize");
    label_currentByte_value->hide();
    label_countSize_value->hide();
    label_currentByte->hide();
    label_countSize->hide();
    
}

MainWindow::~MainWindow()
{
    
    delete centerWidget;
    delete ui;
}

void MainWindow::paintEvent(QPaintEvent *)
{

}
bool MainWindow::nativeEvent(const QByteArray& eventType, void* message, long* result)
{
    MSG* param = static_cast<MSG*>(message);

    switch (param->message)
    {
        case 0x0400+2:
        {
            WPARAM wparam = param->wParam;
            qDebug() << "process:" << wparam;
            if (pushButton_ModeDefault->isChecked())
            {
                if (uefiPushButton->isChecked())
                {
                    progressBar->setValue((int)wparam);
                    progressBar->setMaximum(0x9695 + 1);
                    if (wparam == 0x9695)
                    {
                        downloadThread = new DownloadThread;
                        //关闭界面时，杀死线程
                        connect(this, &MainWindow::destroyed, this, &MainWindow::dealDone);
                        //接收子线程发来的数据
                        connect(downloadThread, &DownloadThread::isDone, this, &MainWindow::receiveMsgFromThread);    //发数据给子线程
                        connect(this, &MainWindow::sengMsgToThread, downloadThread, &DownloadThread::recMegFromMain);
                        sengMsgToThreadBtn();
                        downloadThread->start();
                    }
                }
                else
                {
                    progressBar->setValue((int)wparam);
                    progressBar->setMaximum(0xF850 + 1);
                    if (wparam == 0xF850)
                    {
                        downloadThread = new DownloadThread;
                        //关闭界面时，杀死线程
                        connect(this, &MainWindow::destroyed, this, &MainWindow::dealDone);
                        //接收子线程发来的数据
                        connect(downloadThread, &DownloadThread::isDone, this, &MainWindow::receiveMsgFromThread);    //发数据给子线程
                        connect(this, &MainWindow::sengMsgToThread, downloadThread, &DownloadThread::recMegFromMain);
                        sengMsgToThreadBtn();
                        downloadThread->start();
                    }
                }
            }
            if(pushButton_ModeISO->isChecked())
            {
                progressBar->setMaximum(0xFD6F);
            }
            progressBar->setValue((int)wparam);
        }
        /*case WM_COPYDATA:
        {
            COPYDATASTRUCT* data = reinterpret_cast<COPYDATASTRUCT*>(param->lParam);
            if (data != NULL)
            {
                if (data->cbData == 1)
                {
                    progressBar->setValue(progressBar->maximum());
                }
            }
        }*/
    }

    return QWidget::nativeEvent(eventType, message, result);

}

//给子线程发信号和参数
void MainWindow::sengMsgToThreadBtn()
{
    HWND windID = (HWND)this->winId();//获取当前this窗口的句柄
    QComboBox* comboBoxcid = this->findChild<QComboBox*>("comboBox_chips_model");
    QVariant qvcid = comboBoxcid->currentData();
    QString cid = qvcid.toString();
    QComboBox* comboBox = this->findChild<QComboBox*>("comboBox_select_device");
    QString name = comboBox->currentText();
    int startIndex = name.indexOf("(");
    int endIndex = name.indexOf(")");
    QString path = name.mid(startIndex + 1, endIndex - 1 - startIndex);
    emit this->sengMsgToThread(cid, path, windID);
}
//停止线程调用
void MainWindow::dealDone()
{
    
    //停止线程
    downloadThread->quit();
   

}
//接收子线程函数
void MainWindow::receiveMsgFromThread(int i)
{
    if (i == 1)
    {
        progressBar->setValue(progressBar->maximum());
    }
}

